﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTWB18TOT
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTWB18TOTData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        VVKPOS VKP = new VVKPOS();
        private string fromdndate = "", enddndate = "";
        string LANG = "",ART="",COLOR="",PVH = "";
        int MTR= 0;
        #endregion
        public JLTWB18TOT()
        {
            InitializeComponent();
            this.Name = "成品质量报告";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new JLTWB18TOTData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ART", typeof(string));
            tableUI.Columns.Add("COLOR", typeof(string));
            tableUI.Columns.Add("LANG", typeof(string));
            tableUI.Columns.Add("MTR", typeof(int));
            tableUI.Columns.Add("PVH", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtART.Focus();
                btnEXCEL.IsEnabled = false;
                btnEXCELPVH.IsEnabled = false;
                //txtARTTYPE.Focus();
                /*
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct B.LSNAME1,A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + "　A.ABPFARBE,A.ABPSOLLMENGE1/100 AS WEI, A.ABPSTARTDAT,A.ABPENDDAT,'' AS REMARK, "
                    + " C.ABADIAUFKRS||'-'||TO_NUMBER(C.ABADIAUFLFD)||'-'||TO_NUMBER(C.ABADIAUFPOS) AS DIORDER "
                    + " FROM ABPOS A,LIEFDAT B,ABAUFM C "
                    + " WHERE A.ABPMANDANT = B.LSMANDANT AND A.ABPLIEFNR = B.LSLIEFNR "
                    + " AND A.ABPMANDANT = C.ABAMANDANT AND A.ABPAUFKRS = C.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = C.ABAAUFLFD AND A.ABPAUFPOS = C.ABAAUFPOS "
                    + " AND A.ABPAUFART='PR'  AND A.ABPERLKZ <>'J' AND C.ABAAPOS='0001' "
                 ;
                */
                /*DataSet setResult = new DataSet();
                setResult = m_Data.LoadData(); 
                setUI.Merge(setResult);  
                this.DataContext = setUI;*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    gDg.Visibility = Visibility.Collapsed;
                    gWB.Visibility = Visibility.Collapsed;
                    gMasterDg.Visibility = Visibility.Visible;
                    gWB18.Visibility = Visibility.Visible;
                    btnEXCEL.IsEnabled = true;
                    btnEXCELPVH.IsEnabled = false;
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    ART = txtART.Text.Trim();
                    COLOR = txtCOLOR.Text.Trim();
                    if (Chk.IsChecked==false)
                    {
                        LANG = "V";
                    }
                    if(Chk.IsChecked==true)
                    {
                        LANG = "E";
                    }
                    if (ChkMTR.IsChecked == false)
                    {
                        MTR = 0;
                    }
                    if (ChkMTR.IsChecked == true)
                    {
                        MTR = 20000;
                    }

                    if (ART == "")
                    {
                        MessageBox.Show("请输入花型号！");
                        return;
                    } 
                    if (COLOR == "")
                    {
                        MessageBox.Show("请输入颜色！");
                        return;
                    }
                    PVH = "N";
                    dwUI["ART"] = ART;
                    dwUI["COLOR"] = COLOR;
                    dwUI["LANG"] = LANG;
                    dwUI["MTR"] = MTR;
                    dwUI["PVH"] = PVH;
                    setResult = m_Data.BuildJLTWB18TOTData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["WB18TOT"];
                    WB18.DataContext = setUI.Tables["WB18"];
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new JLTWB18TOTData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            JLTWB18TOTReport m_rpt = new JLTWB18TOTReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.LANGUAGE = LANG;
            m_rpt.ART = ART;
            m_rpt.COLOR = COLOR;
            m_rpt.PVH = PVH;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }

                
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void FROM_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdndate = "";
                fromdndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddndate = "";
                enddndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnQueryPVH_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    gDg.Visibility = Visibility.Visible;
                    gWB.Visibility = Visibility.Visible;
                    gMasterDg.Visibility = Visibility.Collapsed;
                    gWB18.Visibility = Visibility.Collapsed;
                    btnEXCEL.IsEnabled = false;
                    btnEXCELPVH.IsEnabled = true;
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    ART = txtART.Text.Trim();
                    COLOR = txtCOLOR.Text.Trim();
                    if (Chk.IsChecked == false)
                    {
                        LANG = "V";
                    }
                    if (Chk.IsChecked == true)
                    {
                        LANG = "E";
                    }
                    if (ChkMTR.IsChecked == false)
                    {
                        MTR = 0;
                    }
                    if (ChkMTR.IsChecked == true)
                    {
                        MTR = 20000;
                    }

                    if (ART == "")
                    {
                        MessageBox.Show("请输入花型号！");
                        return;
                    }
                    if (COLOR == "")
                    {
                        MessageBox.Show("请输入颜色！");
                        return;
                    }
                    PVH = "J";
                    dwUI["ART"] = ART;
                    dwUI["COLOR"] = COLOR;
                    dwUI["LANG"] = LANG;
                    dwUI["MTR"] = MTR;
                    dwUI["PVH"] = PVH;
                    setResult = m_Data.BuildJLTWB18TOTData(dwUI);
                    setUI.Merge(setResult);
                    Dg.DataContext = setUI.Tables["WB18TOT"];
                    WB.DataContext = setUI.Tables["WB18"];
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string roll = txtROLL.Text.Trim();
                string bat = txtBAT.Text.Trim();
                roll = Helpers.Helper.Left(roll.PadRight(5,' '),5);
                bat = Helpers.Helper.Left(bat.PadRight(5,' '),5);
                DataRow DWVKP = VKP.TBMain.NewRow();
                foreach (DataRow dw in setUI.Tables["WB18TOT"].Rows)
                {
                    DWVKP["VKPAUFKRS"] = dw["VKPAUFKRS"].ToString();
                    DWVKP["VKPAUFLFD"] = dw["VKPAUFLFD"].ToString();
                    DWVKP["VKPAUFPOS"] = dw["VKPAUFPOS"].ToString();
                    DWVKP["VKPMANDANT"] = "00";
                    DWVKP["VKPLIART"] = roll;
                    DWVKP["VKPLIART2"] = bat;
                    VKP.UpdateVKPOS(DWVKP,"JLT",21);
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
